package uitils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

public class UIUtils {

	public static void showTip(Context context, String msg, int durationTime) {
		// 提示框的核心组件文本
		Text text = new Text(context);
		text.setWidth(MATCH_CONTENT);
		text.setHeight(MATCH_CONTENT);
		text.setTextSize(16, Text.TextSizeType.FP);
		text.setText(msg);
		text.setMultipleLine(true);
		text.setPadding(30, 20, 30, 20);
		text.setMarginLeft(30);
		text.setMarginRight(30);
		text.setTextColor(Color.WHITE);
		text.setTextAlignment(TextAlignment.CENTER);

		// 给上面的文本设置一个背景样式
		ShapeElement style = new ShapeElement();
		style.setShape(ShapeElement.RECTANGLE);
		style.setRgbColor(new RgbColor(77,77,77));
		style.setCornerRadius(15);
		text.setBackground(style);

		// 设置布局
		DirectionalLayout mainLayout = new DirectionalLayout(context);
		mainLayout.setWidth(MATCH_PARENT);
		mainLayout.setHeight(MATCH_CONTENT);
		mainLayout.setAlignment(LayoutAlignment.CENTER);
		mainLayout.addComponent(text);


		// 最后要让上面的组件绑定dialog
		ToastDialog toastDialog = new ToastDialog(context);
		toastDialog.setSize(MATCH_PARENT, MATCH_CONTENT);
		toastDialog.setDuration(durationTime);
		toastDialog.setAutoClosable(true);
		toastDialog.setTransparent(true);
		toastDialog.setAlignment(LayoutAlignment.CENTER);
		toastDialog.setComponent(mainLayout);
		toastDialog.show();
	}

	public static void showTip(Context context, String msg) {
		showTip(context, msg, 2000);
	}
}
